package com.study.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Map;

/**
 * 练习定时任务
 */
@Slf4j
public class StudyTask {


    static Integer HOUR = 8;
    static Integer MINUTE = 0;
    static Integer SECOND = 0;


    /**
     * 早餐
     *
     * <p> fixedDelay = 300 表示当前方法执行完毕300ms后，spring scheduling会再次调用该方法 </p>
     */
    @Scheduled(fixedDelay = 3000)
    public static void breakfast() {

        log.info("定时任务执行breakfast方法");

        Map<String, Integer> currentTime = getCurrentTime();
        Integer hour = currentTime.get("hour");
        Integer minute = currentTime.get("minute");
        Integer second = currentTime.get("second");

        if (HOUR.equals(hour) && MINUTE.equals(minute) && SECOND.equals(second)) {
            System.out.println("该吃早餐了");
        }

    }

    /**
     * 开始工作
     * <p> fixedRate = 300 表示当前方法开始执行300ms后，spring scheduling会再次调用该方法 </p>
     */
    @Scheduled(fixedRate = 3000)
    public void work() {

        log.info("定时任务执行work方法");

        HOUR = 10;
        Map<String, Integer> currentTime = getCurrentTime();
        Integer hour = currentTime.get("hour");
        Integer minute = currentTime.get("minute");
        Integer second = currentTime.get("second");

        if (HOUR.equals(hour) && MINUTE.equals(minute) && SECOND.equals(second)) {
            System.out.println("该上班了");
        }
    }


    /**
     * 午餐
     * <p> initialDelay = 100 表示容器启动后，延迟100ms执行第一次任务，以后每200ms执行一次任务 </p>
     */
    @Scheduled(initialDelay = 100, fixedRate = 1000)
    public static void lunch() {
        log.info("定时任务执行lunch方法");
        HOUR = 11;
        MINUTE = 33;
        Map<String, Integer> currentTime = getCurrentTime();
        Integer hour = currentTime.get("hour");
        Integer minute = currentTime.get("minute");
        Integer second = currentTime.get("second");

        if (HOUR.equals(hour) && MINUTE.equals(minute) && SECOND.equals(second)) {
            System.out.println("该吃午餐了");
        }
    }

    /**
     * 晚餐
     *
     * <p>每6秒执行一次，但是执行的方式是与fixedDelay相近的，也是会按照上一次方法结束时间开始算起。</p>
     */
    @Scheduled(cron = " 0 0 0/8 * * ? ")
    public void dinner() {

        log.info("定时任务执行dinner方法");

        HOUR = 17;
        Map<String, Integer> currentTime = getCurrentTime();
        Integer hour = currentTime.get("hour");
        Integer minute = currentTime.get("minute");
        Integer second = currentTime.get("second");

        if (HOUR.equals(hour) && MINUTE.equals(minute) && SECOND.equals(second)) {
            System.out.println("该吃晚餐了");
        }
    }

    /**
     * 睡觉
     *
     * <p>cron = "0 0/1 * * * ? " 表示一分钟执行一次方法</p>
     */
    @Scheduled(cron = "0 0/1 * * * ? ")
    public void sleep() {
        log.info("定时任务执行sleep方法");

        HOUR = 22;

        Map<String, Integer> currentTime = getCurrentTime();
        Integer hour = currentTime.get("hour");
        Integer minute = currentTime.get("minute");
        Integer second = currentTime.get("second");

        if (HOUR.equals(hour) && MINUTE.equals(minute) && SECOND.equals(second)) {
            System.out.println("该睡觉了");
        }

    }

    /**
     * 获取当前时间--时分秒
     *
     * @return
     */
    public static Map<String, Integer> getCurrentTime() {
        Map<String, Integer> map = new HashMap<>();
        Calendar calendar = new GregorianCalendar();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        log.info("{}年{}月{}日", year, month, day);

        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int second = calendar.get(Calendar.SECOND);
        log.info("{}时{}分{}秒", hour, minute, second);

        map.put("year", year);
        map.put("month", month);
        map.put("day", day);
        map.put("hour", hour);
        map.put("minute", minute);
        map.put("second", second);

        return map;
    }

}
